package cn.zl.ymcc.controller;

import cn.zl.ymcc.entity.CourseChapter;
import cn.zl.ymcc.result.PageResult;
import cn.zl.ymcc.result.Result;
import cn.zl.ymcc.service.CourseChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 课程章节 ， 一个课程，多个章节，一个章节，多个视频 前端控制器
 * </p>
 *
 * @author zl
 * @since 2025-10-21
 */
@RestController
@RequestMapping("/courseChapter")
public class CourseChapterController {

        @Autowired
        private CourseChapterService courseChapterService;

        @Operation(summary = "创建课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @PostMapping("/save")
        public Result<Boolean> save(@RequestBody CourseChapter courseChapter) {
            boolean result = courseChapterService.save(courseChapter);
            return result ? Result.success() : Result.error("创建失败");
        }

        @Operation(summary = "更新课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @PutMapping
        public Result<Boolean> update(@RequestBody CourseChapter courseChapter) {
            boolean result = courseChapterService.updateById(courseChapter);
            return result ? Result.success() : Result.error("更新失败");
        }

        @Operation(summary = "删除课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @DeleteMapping("/{id}")
        public Result<Boolean> delete(
                @Parameter(description = "ID", required = true)
                @PathVariable Long id) {
            boolean result = courseChapterService.removeById(id);
            return result ? Result.success() : Result.error("删除失败");
        }

        @Operation(summary = "根据ID查询课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @GetMapping("/listByCourseId/{id}")
        public Result<List<CourseChapter>> findById(
                @Parameter(description = "ID", required = true)
                @PathVariable Long id) {
            QueryWrapper<CourseChapter> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("course_id", id);
            List<CourseChapter> list = courseChapterService.list(queryWrapper);
            return list != null ? Result.success(list) : Result.error("数据不存在");
        }

        @Operation(summary = "查询所有课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @GetMapping("/list")
        public Result<List<CourseChapter>> findAll() {
            List<CourseChapter> list = courseChapterService.list();
            return Result.success(list);
        }

        @Operation(summary = "分页查询课程章节 ， 一个课程，多个章节，一个章节，多个视频")
        @GetMapping("/page")
        public Result<PageResult<CourseChapter>> findPage(
                @Parameter(description = "页码", example = "1")
                @RequestParam(defaultValue = "1") Integer pageNum,
                @Parameter(description = "每页数量", example = "10")
                @RequestParam(defaultValue = "10") Integer pageSize) {
            Page<CourseChapter> page = new Page<>(pageNum, pageSize);
            QueryWrapper<CourseChapter> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");

            IPage<CourseChapter> pageResult = courseChapterService.page(page, queryWrapper);
            PageResult<CourseChapter> result = PageResult.build(
                    pageResult.getTotal(),
                    pageResult.getRecords(),
                    pageNum,
                    pageSize
            );
            return Result.success(result);
        }

        }